﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


using LY.Entity;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;
using LY.Interface;
using LY.Entity.Jsons;
using LY.Entity.Entities;

namespace QJ.CMS.Areas.Admin.Controllers
{
    public class BlackMemberController : Controller
    {
        //
        // GET: /Admin/BlackMember/
        private static IBlackMemberService _blackservice;
        private static IMemberService _memberservice;
        public BlackMemberController(IBlackMemberService bService, IMemberService mService)
        {
            _blackservice = bService;
            _memberservice = mService;
        }

        public ActionResult Index()
        {
            return View();
        }
        //public ActionResult BlackMemberList()
        //{
        //    return View();
        //}
        [HttpPost]
        public JsonResult BlackMemberList(BlackMemberSearch search, FormCollection form)
        {
            PageView view = new PageView(form);
            string colkey = form["colkey"];
            string colsinfo = form["colsinfo"];
            PagedList<U_BlackMember_EX> bList = _blackservice.SelestBlackMember(search, view);
            //JsonQTable at = JsonQTable.ConvertFromList<U_BlackMember>(bList, null, null);
            JsonQTable at = JsonQTable.ConvertFromPagedList<U_BlackMember_EX>(bList, colkey, colsinfo.Split(','));
            return Json(at);
        }

        //添加界面
        public ActionResult BlackMemberAdd(int id)
        {
            U_BlackMember blackMember = null;
            blackMember = new U_BlackMember();
            blackMember.MemberID = id;
            blackMember.AddedTime = DateTime.Now;

            return View(blackMember);
        }
        //修改界面
        public ActionResult BlackMemberEdit(int? id)
        {
            U_BlackMember blackMember = _blackservice.getBlackMember(id.Value);
            return View(blackMember);

        }


        [HttpPost]
        [ValidateInput(false)]
        public JsonResult blackMemberSave(U_BlackMember blackMember)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                long ret;
                if (blackMember.ID == 0) //新曾
                {
                    ret = _blackservice.AddBlackMember(blackMember);
                    // var MODEL = _memberservice.GetMember(blackMember.ID);
                    //MODEL.Status = (int)LY.Common.Enum.UserStatus.黑名单;
                    // U_BlackMember blackmember = _blackservice.getBlackMember(id);

                    U_Member member = _memberservice.getMemberInfo(blackMember.MemberID);
                    member.Status = 4;
                    ret = _blackservice.updateMemberStutas(member);

                }
                else
                {
                    ret = _blackservice.UpdateBlackMember(blackMember);
                }
                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Message = "操作成功！";
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }
        private static bool Validate(U_BlackMember blackMember, out string errmsg)
        {
            errmsg = "";

            var loginUserInfo = System.Web.HttpContext.Current.Session["LoginUseInfo"];
            blackMember.MemberID = ((LY.Entity.U_BlackMember)loginUserInfo).MemberID;
            blackMember.AddedTime = ((LY.Entity.U_BlackMember)loginUserInfo).AddedTime;
            blackMember.Remark = ((LY.Entity.U_BlackMember)loginUserInfo).Remark;
            return true;
        }
        //删除黑名单
        [HttpPost]
        public JsonResult BlackMemberDelete(int id)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                if (id <= 0)
                {
                    rMsg.Status = -1;
                    rMsg.Message = "参数错误！";
                }
                U_BlackMember blackmember = _blackservice.getBlackMember(id);

                U_Member member = _memberservice.getMemberInfo(blackmember.MemberID);
                member.Status = 1;

                int ret = _blackservice.updateMemberStutas(member);
                //int ret = _memberservice.updateMemberStatus(member.MemberID, 1);
                //int ret = _memberservice.updateMemberStatus(id, 1);
                ret = _blackservice.DeleteBlackMember(member.ID);


                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Message = "操作成功！";
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {

                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }


    }
}
